<template>
  <div class="my-page" v-if="designerInfo">
    <Scroll class="myPage-scroll" :data="designerInfo">
      <div>
        <my-home-page-cmp @worksDetail="handleWorksDetail" :designerInfo="designerInfo">
          <template v-slot:edit>
            <div class="edit-wrapper">
              <span @click="$router.push('/my/my-homepage/release')">
                <i class="add"></i>
              </span>
              <span @click="$router.push('/my/my-homepage/edit-msg')">
                <i class="edit-msg"></i>
              </span>
              <span class="my-tip work" v-if="showWorkTip">添加作品</span>
              <span class="my-tip statistic" v-if="showStatisticTip">编辑资料</span>
            </div>
          </template>
        </my-home-page-cmp>
      </div>
    </Scroll>
    <transition name="slide">
      <router-view @success="handleSuccess"></router-view>
    </transition>
  </div>
</template>
<script>
import MyHomePageCmp from '@/components/my-homepage-cmp/my-homepage-cmp'
import Scroll from '@/components/scroll/scroll'
import { addWorks, getDesignerDetail } from '@/api/home'
import EditMsg from '@/views/my/mixins/edit-msg'
const ERR_OK = 200
export default {
  components: {
    MyHomePageCmp,
    Scroll
  },
  data () {
    return {
      showWorkTip: true,
      showStatisticTip: false
    }
  },
  mixins: [EditMsg],
  created () {
    if (localStorage.getItem('showWorkTip')) {
      this.showWorkTip = JSON.parse(localStorage.getItem('showWorkTip'))
    }
    if (this.showWorkTip) {
      const timer1 = setTimeout(() => {
        localStorage.setItem('showWorkTip', false)
        this.showWorkTip = false
        clearTimeout(timer1)
        this.showStatisticTip = true
        const time2 = setTimeout(() => {
          this.showStatisticTip = false
          clearTimeout(time2)
        }, 3000)
      }, 3000)
    }
  },
  computed: {
    userInfo () {
      return this.$store.getters.userInfo
    }
  },
  beforeRouteEnter (to, from, next) {
    // ...
    next()
  },
  methods: {
    handleSuccess () {
      this._getDesignerDetail(this.userInfo.designer_id)
    },
    handleWorksDetail (item) {
      this.$router.push({
        path: '/my/my-homepage/case-detail',
        query: { id: item.id }
      })
    },
    _getDesignerDetail (id) {
      getDesignerDetail(id)
        .then(res => {
          if (res.code === ERR_OK) {
            this.designerInfo = res.data
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _addWorks ({ cover, content, title, images }) {
      addWorks({ cover, content, title, images })
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/mixin.scss";
@include slideEnter();
.my-tip {
  padding: 0.16rem 0.26rem !important;
  font-size: 0.18rem !important;
  line-height: 0.18rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(255, 255, 255, 1);
  background: rgba(0, 4, 0, 0.8);
  border-radius: 0.1rem 0rem 0.1rem 0.1rem;
  white-space: nowrap;
  &.work {
    position: absolute;
    top: 0.48rem;
    right: 1.4rem;
  }
  &.statistic {
    position: absolute;
    top: 0.48rem;
    right: 0.26rem;
  }
}
.my-page {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: #f7f7f7;
  touch-action: none;
  z-index: 8;
  .myPage-scroll {
    width: 100%;
    height: 100%;
  }
  .edit-wrapper {
    display: flex;
    align-items: center;
    position: relative;
    span {
      padding: 0.1rem;
      font-size: 0;
      &:nth-of-type(1) {
        margin-right: 0.6rem;
      }
    }
    .add {
      display: inline-block;
      width: 0.36rem;
      height: 0.36rem;
      background: url("../../assets/images/icon_tjzp.png") no-repeat;
      background-size: cover;
    }
    .edit-msg {
      display: inline-block;
      width: 0.32rem;
      height: 0.32rem;
      background: url("../../assets/images/icon_bianji.png") no-repeat;
      background-size: cover;
    }
  }
}
</style>
